var piCompanyAssessment = db.PICompanyAssessment.find().toArray();

piCompanyAssessment.forEach(function(piCaItem)
{
    if (piCaItem.Indicators)
    {
        piCaItem.Indicators.forEach(function(caIndicator)
        {
            var fwIndicator = db.PIIndicator.findOne({Symbol: caIndicator.IndicatorSymbol});            
            if (fwIndicator)
            {                 
                var fwCateg = fwIndicator.Categories.find(function(x){return x.Symbol == caIndicator.CategorySymbol});                    
                if (fwCateg)
                {
                    if (fwCateg.RevenueRangeYesNo)
                    {
                        var fwRRange = fwCateg.RevenueRange.find(function(x){return x[1].RangeId == caIndicator.LevelOfInvolvement._id});
                        if (fwRRange)
                            caIndicator.Score = fwRRange[1].Score;
                    }
                    else
                    {
                        caIndicator.Score = fwCateg.Score;
                    }                    
                }

                

                db.PICompanyAssessmentShortReport.update(
                    { CompanyId:piCaItem._id, CategorySymbol:caIndicator.CategorySymbol },
                    { $set: { Score: caIndicator.Score }},
                    { upsert: false, multi: true });

            }
        });

        
        db.PICompanyAssessment.save(piCaItem);        
    }    
});

